package reader.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import reader.entity.Member;
import reader.entity.MemberReadState;
import reader.mapper.MemberMapper;
import reader.mapper.MemberReadStateMapper;
import reader.service.MemberService;
import reader.service.exception.MemberException;
import reader.utils.MD5Utils;

import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
@Transactional(propagation = Propagation.NOT_SUPPORTED,readOnly = true)
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private MemberReadStateMapper memberReadStateMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Member createMember(String name, String password, String nickName) {
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", name);
        List<Member> members = memberMapper.selectList(queryWrapper);
        if(!members.isEmpty()){
            throw new MemberException("用户已存在");
        }
        Member member = new Member();
        member.setUsername(name);
        member.setNickname(nickName);
        member.setCreateTime(new Date());
        int salt = new Random().nextInt(1000) + 1000;
        member.setSalt(salt);
        String s = MD5Utils.md5(password, salt);
        member.setPassword(s);
        memberMapper.insert(member);
        return member;
    }

    @Override
    public Member login(String name, String password) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", name);
        Member members = memberMapper.selectOne(queryWrapper);
        if(members == null){
            throw new MemberException("没有该用户");
        }
        String s = MD5Utils.md5(password, members.getSalt());
        if(!s.equals(members.getPassword())){
            throw new MemberException("密码有误");
        }

        return members;
    }

    @Override
    public Member getMember(Long id) {
        return memberMapper.selectById(id);
    }

    @Override
    public MemberReadState getMemberReadState(Long id,Long bookId) {
        QueryWrapper<MemberReadState> wrapper = new QueryWrapper<>();
        wrapper.eq("book_id", bookId);
        wrapper.eq("member_id", id);
        return memberReadStateMapper.selectOne(wrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public MemberReadState updateMemberReadState(Long id, Long bookId, Integer readState) {
        QueryWrapper<MemberReadState> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("book_id", bookId);
        queryWrapper.eq("member_id", id);
        MemberReadState memberReadState = memberReadStateMapper.selectOne(queryWrapper);
        if(memberReadState == null){
            // 没看过
            memberReadState = new MemberReadState();
            memberReadState.setMemberId(id);
            memberReadState.setReadState(readState);
            memberReadState.setCreateTime(new Date());
            memberReadState.setBookId(bookId);
            memberReadStateMapper.insert(memberReadState);
        }else{
            memberReadState.setReadState(readState);
            memberReadState.setCreateTime(new Date());
            memberReadStateMapper.updateById(memberReadState);
        }
        return memberReadState;
    }


}
